<%--
  Created by IntelliJ IDEA.
  User: A
  Date: 2024/4/26
  Time: 14:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css">
    <script src="../../static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <br>
    <form class="layui-form layui-row layui-col-space16 layui-inline" style="margin-left: 20px">
        <div class="layui-inline" style="width: 100px;">管理员用户名</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <input type="text" name="username" value="" placeholder="请输入" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-inline" style="width: 90px;">管理员账号</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <input type="text" name="account" placeholder="请输入" lay-affix="clear" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 90px;">管理员角色</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <select id="roleId" name="roleId" lay-filter="roleId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">Search</button>
            <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
    <%--头部工具条--%>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container layui-inline">
            <button class="layui-btn" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>
        <%--搜索form表单--%>

    </script>
    <%--右侧工具条--%>
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        </div>
    </script>
    <script type="text/html" id="imageTemplet">
        <img src="/pic/{{d.avatar}}">
    </script>
    <script type="text/html" id="statusTemplet">
        <input type="checkbox" name="status" value="{{= d.id }}" title="启用|封禁"
               lay-skin="switch" lay-filter="statusFilter" {{= d.status == 1 ? "checked" : "" }}>
    </script>
    <script type="text/html" id="roleTemplet">
        {{#     if (d.roleId == 1) {                }}
        <button type="button" class="layui-btn layui-bg-red layui-btn-sm">超级管理员</button>
        {{#     } else if (d.roleId == 2) {         }}
        <button type="button" class="layui-btn layui-bg-orange layui-btn-sm">普通管理员</button>
        {{#     } else if (d.roleId == 3) {         }}
        <button type="button" class="layui-btn layui-btn-sm">汽车管理员</button>
        {{#     } else if (d.roleId == 4) {         }}
        <button type="button" class="layui-btn layui-bg-purple layui-btn-sm">用户管理员</button>
        {{#     } else if (d.roleId == 5) {         }}
        <button type="button" class="layui-btn layui-bg-blue layui-btn-sm">订单管理员</button>
        {{#     }                                   }}
    </script>
    <script type="text/html" id="permissionTemplet">
        {{#  var permissionName = d.permissionName }}
        <select name="city" class="layui-border select-demo-primary" lay-ignore>
            <option value="">查看此管理员权限</option>
            {{#  layui.each(permissionName, function(i, v){ }}
            <option value="{{= v }}" {{= v === d.permissionName ? 'selected' : '' }}>{{= v }}</option>
            {{#  }); }}
        </select>
    </script>
    <script>
        layui.use(['form','table'], function(){
            var table = layui.table;
            var form = layui.form;
            // 创建渲染实例
            table.render({
                elem: '#test',
                id: 'tableId',
                url:'/admin?method=selectByPage',
                toolbar: '#toolbarDemo',
                page: true,
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', width:60, title: 'ID', sort: true},
                    {field:'username', title: '用户名', width: 80},
                    {field:'avatar', title: '头像', templet: '#imageTemplet', width: 120},
                    {field:'account', title: '账号',  width: 120},
                    {field:'password', title: '密码', width:120},
                    {field:'status', title: '状态', templet: '#statusTemplet', align: 'center', width: 100},
                    {field:'gender', title: '性别', align: 'center',width: 60},
                    {field:'roleName', title: '管理员角色', align: 'center', templet: '#roleTemplet',width: 120},
                    {field: 'permissionName', title: '管理员权限', width:180, unresize: true, align: 'center', templet: '#permissionTemplet'},
                    {field: 'age', title: '年龄', width: 85, align: 'center', sort: true},
                    {field: 'email', title: '邮箱', width: 200},
                    {fixed: 'right', title:'操作', width: 100, align: 'center', minWidth: 125, toolbar: '#barDemo'}
                ]],
                done: function(res, curr, count){
                    var options = this;

                    // 获取当前行数据
                    table.getRowData = function(tableId, elem){
                        var index = $(elem).closest('tr').data('index');
                        return table.cache[tableId][index] || {};
                    };

                    // 原生 select 事件
                    var tableViewElem = this.elem.next();
                    tableViewElem.find('.select-demo-primary').on('change', function(){
                        var value = this.value; // 获取选中项 value
                        var data = table.getRowData(options.id, this); // 获取当前行数据(如 id 等字段，以作为数据修改的索引)
                        // 更新数据中对应的字段
                        data.city = value;
                        // 显示 - 仅用于演示
                    });
                }
            });
            $.post(
                '/role?method=selectAll',
                function (result) {
                    if (result.code == 0) {
                        $(result.data).each(function () {
                            $('#roleId').append('<option value="'+this.id+'">'+this.roleName+'</option>');
                        })
                        form.render('select');
                    }
                },
                'json'
            );
            form.on('switch(statusFilter)', function(obj){
                //update blog set status=? where id=1;
                var id = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 1 : 0;
                $.post(
                    '/admin?method=updateStatus',
                    {'id': id, 'status': status},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        }
                    },
                    'json'
                );
            });
            form.on('submit(submitSearch)', function(data){
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('tableId', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });
            table.on('toolbar(test)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'add':
                        var index = layer.open({
                            title: '管理员注册',
                            type: 2,
                            area: ['35%', '90%'],// 宽高
                            content: '/page/admin/add',
                        });
                        break;
                    case 'deleteAll':
                        var data = checkStatus.data;
                        //[{"id":2,"name":"Java1812","address":"ddf","time":"232"},{"id":3,"name":"Java1903","address":"34","time":"323"}]
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        if (ids.includes(1)) {
                            layer.confirm(
                                '此管理员不可删除！',
                                {icon : 2},
                            )
                        } else {
                            layer.confirm(
                                '您确认要删除么?',
                                {icon : 3},
                                function(index){
                                    $.post(
                                        '/admin?method=deleteAll',
                                        {'ids': ids},
                                        function (result) {
                                            console.log(result);
                                            if (result.code == 0) {
                                                mylayer.okMsg(result.msg);
                                                //删除成功之后刷新表格，展示最新数据
                                                table.reload('tableId');
                                            } else {
                                                mylayer.errorMsg(result.msg);
                                            }
                                        },
                                        'json'
                                    );
                                }
                            );
                        }
                        break;
                    case 'LAYTABLE_TIPS':
                        layer.alert('自定义工具栏图标按钮');
                        break;
                };
            });
            table.on('tool(test)', function(obj){ // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                console.log(obj);
                if(obj.event === 'delete'){
                    if (data.id === 1) {
                        layer.confirm(
                            '此管理员不可删除！',
                            {icon : 2},
                        )
                    } else {
                        layer.confirm(
                            '您确定要删除吗？',
                            {icon : 3},
                            function (index) {
                                $.post(
                                    '/admin?method=deleteById',
                                    {'id':data.id},
                                    function (result) {
                                        console.log(result);
                                        if (result.code == 0) {
                                            mylayer.okMsg(result.msg);
                                            table.reload('tableId');
                                        } else {
                                            mylayer.errorMsg(result.msg);
                                        }
                                    },
                                    'json'
                                );
                            }
                        );
                    }

                } else if(obj.event === 'edit'){
                    var index = layer.open({
                        title: '编辑博客',
                        type: 2,
                        area: ['35%', '90%'],// 宽高
                        content: '/page/admin/update?id='+data.id,
                    });
                }
            });
        });
    </script>
</body>
</html>
